package ru.lukyanets.transmission.protocol.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.io.Serial;
import java.util.Base64;
import java.util.BitSet;

/**
 * @author Sergey Lukyanets
 */
public class PiecesDeserializer extends StdDeserializer<BitSet> {

    @Serial
    private static final long serialVersionUID = 8686890682541973441L;

    protected PiecesDeserializer() {
        super(BitSet.class);
    }

    @Override
    public BitSet deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = p.getValueAsString();
        return value != null ? BitSet.valueOf(Base64.getDecoder().decode(value.getBytes())) : null;
    }
}
